package com.ruoyi.web.controller.ydz;

import java.util.List;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.Drug;
import com.ruoyi.web.service.IDrugService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 药品管理Controller
 * 
 * @author drx
 * @date 2025-05-13
 */
@Controller
@RequestMapping({"/ydz/drug","/api/drug"})
@Api(tags = "药品管理")
public class DrugController extends BaseController
{
    private String prefix = "ydz/drug";

    @Autowired
    private IDrugService drugService;

    @RequiresPermissions("ydz:drug:view")
    @GetMapping()
    public String drug()
    {
        return prefix + "/drug";
    }

    /**
     * 查询药品管理列表
     */
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("查询药品管理列表")
    public TableDataInfo list(Drug drug)
    {
        startPage();
        List<Drug> list = drugService.selectDrugList(drug);
        return getDataTable(list);
    }

    @PostMapping("/getDruglist")
    @ResponseBody
    @ApiOperation("查询药品管理列表")
    public TableDataInfo getDruglist(@RequestBody Drug drug)
    {
        startPage();
        List<Drug> list = drugService.selectDrugList(drug);
        return getDataTable(list);
    }

    /**
     * 导出药品管理列表
     */
    @RequiresPermissions("ydz:drug:export")
    @Log(title = "药品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    @ApiOperation("导出药品管理列表")
    public AjaxResult export(Drug drug)
    {
        List<Drug> list = drugService.selectDrugList(drug);
        ExcelUtil<Drug> util = new ExcelUtil<Drug>(Drug.class);
        return util.exportExcel(list, "药品管理数据");
    }

    /**
     * 新增药品管理
     */
    @GetMapping("/add")
    @ApiOperation("新增药品管理")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存药品管理
     */
    @ApiOperation("新增保存药品管理")
    @RequiresPermissions("ydz:drug:add")
    @Log(title = "药品管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody Drug drug)
    {
        return toAjax(drugService.insertDrug(drug));
    }

    /**
     * 修改药品管理
     */
    @ApiOperation("修改药品管理")
    @RequiresPermissions("ydz:drug:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Drug drug = drugService.selectDrugById(id);
        mmap.put("drug", drug);
        return prefix + "/edit";
    }

    /**
     * 修改保存药品管理
     */
    @ApiOperation("修改保" +
            "存药品管理")
    @RequiresPermissions("ydz:drug:edit")
    @Log(title = "药品管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody Drug drug)
    {
        return toAjax(drugService.updateDrug(drug));
    }

    /**
     * 删除药品管理
     */
    @ApiOperation("删除药品管理")
    @RequiresPermissions("ydz:drug:remove")
    @Log(title = "药品管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(drugService.deleteDrugByIds(ids));
    }

    /**
     * 新增保存药品管理
     */
    @ApiOperation("获取药品详情")
    @Log(title = "药品管理", businessType = BusinessType.INSERT)
    @GetMapping("/getDrugById/{id}")
    @ResponseBody
    public AjaxResult getDrugById(@PathVariable("id") Long id)
    {
        AjaxResult ajaxResult = new AjaxResult();
        try{
            Drug drug = drugService.selectDrugById(id);
            ajaxResult.put("data",drug);
            ajaxResult.put("code",0);
            ajaxResult.put("msg","查询成功！");
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code","500");
            ajaxResult.put("msg","获取药品详情失败，请联系管理员！");
        }
        return ajaxResult;
    }

    /*
    * 修改药品状态
    * */
    @Log(title = "药品管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editState")
    @ResponseBody
    public AjaxResult editState(@RequestBody Drug drug){
        return toAjax(drugService.updateDrug(drug));
    }
}
